package com.bot.nft.module.dc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bot.nft.core.enums.cm.Enum;
import com.bot.nft.module.dc.domain.DcAuthorInfo;
import com.bot.nft.module.dc.domain.bo.DcAuthorInfoBo;
import com.bot.nft.module.dc.domain.vo.DcAuthorInfoVo;
import com.bot.nft.module.dc.mapper.DcAuthorInfoMapper;
import com.bot.nft.module.dc.service.DcAuthorInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * dc_author_infoService接口实现类
 *
 * @author lumengli
 * @date 2024/01/24 15:57
 **/
@Service
public class DcAuthorInfoServiceImpl extends ServiceImpl<DcAuthorInfoMapper, DcAuthorInfo> implements DcAuthorInfoService {

    @Resource
    private DcAuthorInfoMapper dcAuthorInfoMapper;

    @Override
    public Page<DcAuthorInfoVo> page(String keyword, Integer status, Integer pageNo, Integer pageSize) {
        //开启分页
        Page<DcAuthorInfo> page = new Page<>(pageNo, pageSize);
        QueryWrapper<DcAuthorInfo> queryWrapper = new QueryWrapper<>();
        //判断关键字不为空
        if (!StringUtils.isEmpty(keyword)) {
            queryWrapper.lambda().like(DcAuthorInfo::getName, keyword);
        }
        //判断状态不为空
        if (null != status) {
            queryWrapper.lambda().like(DcAuthorInfo::getStatus, status);
        }
        queryWrapper.lambda().like(DcAuthorInfo::getDeleteFlag, Enum.NODEL.getValue());
        //查询结果
        Page<DcAuthorInfoVo> dcAuthorInfoPage = dcAuthorInfoMapper.selectVoPage(page, queryWrapper);
        return dcAuthorInfoPage;
    }

    @Override
    public Boolean addAuthorDcInfo(DcAuthorInfoBo dcAuthorInfoBo) {
        dcAuthorInfoBo.setId(IdUtil.getSnowflakeNextIdStr());
        dcAuthorInfoBo.setDeleteFlag(0);
        if (dcAuthorInfoBo.getStatus().equals("")){
            dcAuthorInfoBo.setStatus(Enum.END.getValue());
        }
        DcAuthorInfo dcAuthorInfo = BeanUtil.toBean(dcAuthorInfoBo, DcAuthorInfo.class);
        int insert = dcAuthorInfoMapper.insert(dcAuthorInfo);
        Boolean msg = false;
        if (insert <= 1) {
            msg = true;
        }
        return msg;
    }

    @Override
    public Boolean startAuthorDcInfo(String id) {
        boolean update = this.lambdaUpdate()
                .set(DcAuthorInfo::getStatus, Enum.START.getValue())
                .eq(DcAuthorInfo::getId, id)
                .update();
        return update;
    }

    @Override
    public Boolean disAuthorDcInfo(String id) {
        boolean update = this.lambdaUpdate()
                .set(DcAuthorInfo::getStatus, Enum.END.getValue())
                .eq(DcAuthorInfo::getId, id)
                .update();
        return update;
    }

    @Override
    public Boolean updateAuthorDcInfo(DcAuthorInfoBo authorDcInfoBo) {
        DcAuthorInfo dcAuthorInfo = BeanUtil.toBean(authorDcInfoBo, DcAuthorInfo.class);
        int i = dcAuthorInfoMapper.updateById(dcAuthorInfo);
        Boolean msg = false;
        if (i == 1) {
            msg = true;
        }
        return msg;
    }

    @Override
    public Boolean delAuthorDcInfo(String id) {
        boolean update = this.lambdaUpdate()
                .set(DcAuthorInfo::getDeleteFlag,Enum.ALDEL.getValue())
                .eq(DcAuthorInfo::getId, id)
                .update();
        return update;
    }

    @Override
    public DcAuthorInfoVo getAuthorDcInfo(String id) {

        DcAuthorInfoVo dcAuthorInfoVo = dcAuthorInfoMapper.selectVoById(id);

        return dcAuthorInfoVo;
    }
}